<template>
  <div class="login-container">
    <div class="login-form-box">
      <div style="flex: 1">
        <img src="@/assets/GIRL WORKING ONLINE.svg" alt="" style="width: 100%">
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center">
        <el-form :model="user" label-width="100px" style="padding-right: 30px;width: 80%" class="aa" :rules="rules" ref="loginRef">
          <div style="text-align: center; font-weight: bold;font-size: 20px; margin-bottom: 20px">欢迎登录后台管理系统</div>
          <el-form-item prop="username">
            <el-input v-model="user.username" prefix-icon="el-icon-user" placeholder="请输入账号" size="medium"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="user.password" prefix-icon="el-icon-lock" show-password placeholder="请输入密码" size="medium"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex">
              <el-input prefix-icon="el-icon-circle-check" v-model="user.code" placeholder="请输入验证码" size="medium" style="width: 50%;margin-right: 20px; flex: 1"></el-input>
              <div style="flex: 1; height: 36px">
                <valid-code @update:value="getCode" />
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" size="medium" @click="login">登 录</el-button>
          </el-form-item>
          <el-form-item>
            <div style="display: flex">
              <div style="flex: 1;">还没有账号？请 <span style="color: #0f9876;cursor: pointer" @click="$router.push('/register')">注册</span></div>
              <div style="flex: 1;text-align: right"><span style="color: #6e77f2;cursor: pointer">忘记密码</span></div>
            </div>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>
</template>

<style scoped>
.login-container{
  background-color: #0f9876;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form-box{
  background-color: white;
  width: 50%;
  padding: 40px 0 40px 0;
  //height: 45%;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
}
.aa >>> .el-form-item__content {
  margin-left:0px !important;
}
</style>

<script>

import ValidCode from "@/components/ValidCode.vue";


export default {
  name: "Login",
  data(){

    //验证码校验
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if (value.toLowerCase() !== this.code){
        callback(new Error('验证码错误'))
      }else {
        callback()
      }

    };

    return {
      code:"",  //验证码组件传递过来的code
      user:{
        code:"",  //表单里用户输入的code
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        code: [
          { validator: validateCode, trigger: 'blur' }
        ],

      }
    }
  },
  components:{
    ValidCode
  },
  created() {

  },
  methods: {
    getCode(code) {
      this.code = code.toLowerCase()
    },
    login(){
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          //验证通过
          this.$request.post('/login', this.user).then(res => {
            if (res.code == '200'){
              this.$router.push('/')
              this.$message.success('登录成功')
              localStorage.setItem("honey-user", JSON.stringify(res.data))  //存储用户数据
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },


  }
}
</script>